<template>
	<view class="update-box">
		<view class="update">
			<image class="logo" src="@/static/images/logo.png" mode="widthFix"></image>
			
			<text class="title text-xl">更新中...</text>
			<view class="cu-progress round sm striped active" style="height: 34rpx; width: 400rpx;">
				<view class="bg-green" :style="`width:${percent}%`">{{percent}}%</view>
			</view>
			<view class="tail">
				<text>{{current}}MB / {{total}}MB</text>
			</view>
		</view>
		<u-no-network></u-no-network>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				percent:0,
				current:0,
				total:0
			}
		},
		onLoad(e) {
			// #ifdef APP-PLUS
			if(e.link) this.update(e.link)
			// #endif
		},
		methods:{
			// 开始更新
			update(link){
				let download=uni.downloadFile({
					url:link,
					success: (e) => {
						if(e.statusCode==200){
							this.install(e.tempFilePath)
						}else{
							this.$toast('下载失败')
						}
					}
				})
				
				download.onProgressUpdate(res=>{
					this.percent=res.progress
					this.current=(res.totalBytesWritten/1000/1000).toFixed(2)
					this.total=(res.totalBytesExpectedToWrite/1000/1000).toFixed(2)
				})
			},
			
			// 开始安装
			install(path){
				plus.runtime.install(path,{force:true},()=>{  
				    plus.runtime.restart();  
				},()=>{  
				    this.$toast('安装失败')
				});
			}
		}
	}
</script>

<style lang="scss">
.update-box{
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	height: 100vh;
	
	.update{
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		margin-bottom: 100rpx;
		
		.title{
			margin-bottom: 20rpx;
		}
		
		.logo{
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 150rpx;
		}
		.ios{
			background-color: #2cc06c;
			text{
				color: white;
				font-size: 30rpx;
				margin-left: 15rpx;
			}
		}
		
		.android{
			background-color: #FFFFFF;
			box-sizing: border-box;
			border: 1px solid #dddddd;
			margin-top: 40rpx;
			text{
				color: #666666;
				font-size: 30rpx;
				margin-left: 15rpx;
			}
		}
		
		.tail{
			width: 400rpx;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			margin-top: 10rpx;
			font-size: 24rpx;
			margin-bottom: 100rpx;
		}
	}
}
</style>
